import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
import storage from './storage'
import {get} from 'lodash'
moment.locale('zh-cn')
export const getVehicleTypeName = (data, separator = '/') => {
  const dictList = storage.get('dictList') || []
  const transmissionTypeEnums = get(dictList.find(e => e.categoryCode === 'TransmissionType'), 'dictList') || []
  const transmissionTypeName = get(transmissionTypeEnums.find(e => e.value === data.transmissionType), 'name')
  return [
    { text: data.vehicleTypeId, unit: '' },
    { text: data.brandName, unit: '' },
    { text: data.name, unit: '' },
    { text: transmissionTypeName, unit: '' },
    { text: data.groupName, unit: '' },
    { text: data.outputVolumn, unit: data.outputType },
    // { text: data.doorNum, unit: '门' },
    { text: data.passengerNum, unit: '座' },
    { text: data.modelYear, unit: '' },
    { text: data.vehiclePlateName, unit: '' }
  ].filter(e => e.text)
  .map(e => e.text + e.unit)
  .join(separator)
}
Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('formatHours', hours => {
  const days = parseInt(hours / 24)
  const remainHours = parseInt(hours % 24)
  if (days === 0) return remainHours + '小时'
  return `${days}天${remainHours ? remainHours + '小时' : ''}`
})
Vue.filter('formatTime', (dateStr, pattern = 'YYYY-MM-DD HH:mm') => {
  return moment(dateStr).format(pattern)
})
Vue.filter('getVehicleTypeName', getVehicleTypeName)
